<?php

namespace app\uniapp\controller;

use app\common\exception\MiException;
use app\model\store\GoodsCart;
use app\model\store\GoodsCate;
use app\Request;
use app\uniapp\middleware\CheckLogin;
use app\uniapp\service\GoodsCartService;
use app\uniapp\service\GoodsCateService;
use app\uniapp\service\GoodsService;
use app\uniapp\service\UniAuthService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class GoodsCartController extends UniController
{
    protected array $middleware = [
        CheckLogin::class,
    ];

    /**
     * @throws DbException|MiException
     */
    public function paginate(Request $request): \think\response\Json
    {
        $limit = $request->param('limit');
        $list = GoodsCart::where('site_id', UniAuthService::instance()->getSiteId())->where('user_id', UniAuthService::instance()->getLoginId())->filter(function ($v) {
            $v->goods = GoodsService::instance()->getDetailById($v->goods_id, $v->goods_sku_id);
            $v->isValid = true; // 购物车中的商品是否有效
            if ( !$v->goods || ($v->goods_sku_id && !$v->goods->sku) ){
                $v->isValid = false;
            }

        })->paginate($limit);
        return jsonSuccess([
            'items' => $list->items(),
            'total' => $list->total(),
        ]);
    }


    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function add(): \think\response\Json
    {
        $goodsId = $this->post('goods_id',0);
        $goodsSkuId = $this->post('goods_sku_id', 0);
        $num = $this->post('num',0);
        if (!$goodsId){
            throw new MiException("没有选择商品");
        }
        $cart = GoodsCart::where("site_id", UniAuthService::instance()->getSiteId())->where('user_id', UniAuthService::instance()->getLoginId())
            ->where('goods_id', $goodsId)->where('goods_sku_id', $goodsSkuId)->find();
        if ($num == 0){ // 从购物车中删除
            $cart?->delete();
        }else{
            if ($cart){ // 更新
                $cart->num = $num;
                $cart->save();
            }else{ // 添加
                $goods = GoodsService::instance()->getDetailById($goodsId, $goodsSkuId);
                GoodsCart::create([
                    'site_id' => UniAuthService::instance()->getSiteId(),
                    'user_id' => UniAuthService::instance()->getLoginId(),
                    'goods_id' => $goodsId,
                    'goods_sku_id' => $goodsSkuId,
                    'goods_title' => $goods->title,
                    'goods_image' => $goodsSkuId ? $goods->sku->image : $goods->image,
                    'num' => $num,
                ]);
            }
        }
        // 购物车商品总数
        $count = GoodsCartService::instance()->getUserTotal();

        return jsonSuccess([
            'num' => $count,
        ]);
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function del(): \think\response\Json
    {
        $ids = $this->post('ids');
        GoodsCartService::instance()->setWhere([
            ['id','in', $ids]
        ])->delete();
        // 购物车商品总数
        $count = GoodsCartService::instance()->getUserTotal();
        return jsonSuccess([
            'num' => $count
        ]);
    }

}
